package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.TestPlanModel;
import org.testis.gui.mvc.view.testdesign.TestPlanView;
import org.testis.orm.testbed.refined.DeviceGroupFactory;
import org.testis.orm.testbed.refined.DeviceGroupRefined;
import org.testis.orm.testbed.refined.DutRefined;
import org.testis.orm.testbed.refined.TestPcFactory;
import org.testis.orm.testbed.refined.TestPcRefined;
import java.util.HashMap;
import java.util.List;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestPlanRefined;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.orm.testdesign.TestPartSaver;
import org.testis.utils.tree.TreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class TestPlanController extends AbstractController implements SaveAble, Addable {

    public static final String CHANGE_NAME = "Name";
    public static final String CHANGE_DESCRIPTION = "Description";
    public static final String CHANGE_TIMEOUT = "Timeout";
    public static final String CHANGE_BROWSER_SPEED = "BrowserSpeed";
    public static final String CHANGE_BROWSER_TYPE = "BrowserType";
    public static final String CHANGE_DUT = "Dut";
    public static final String CHANGE_PC = "Pc";
    public static final String CHANGE_TEST_BED = "TestBed";
    public static final String CHANGE_DUT_IP = "DutIp";
    public static final String CHANGE_LANGUAGE_ID = "LanguageId";
    public static final String CHANGE_PROFILE_ID = "ProfileId";
    public static final String SHOW_ADD_TEST_PLAN = "TestPlanVisibility";
    private TestDesignTreeNode m_selectedNode = null;

    public TestPlanController() {
        super();
    }

    public void operationNewTestPlan() {
        initialize(false);
        m_selectedNode = TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode();
        setModelProperty(SHOW_ADD_TEST_PLAN,
                true);
    }

    public void operationEditTestPlan(TestDesignTreeNode node) {
        if (node.getReference().getType() == null) {
            System.out.println("Not found reference type for test plan");
            return;
        } else if (node.getReference().getType().getName() == null) {
            System.out.println(
                    "Not found reference type name for test plan");
            return;
        }
        if (node.getReference().getType().getName().equals("TestPlan")) {
            initialize(true);
            TestPlanRefined designpart = null;
            try {
                designpart = (TestPlanRefined) node.getObject();
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
            if (designpart == null) {
                System.out.println("Not found design part for test plan");
                return;
            }
            setModelProperty(CHANGE_NAME,
                    designpart.getName().toString());
            setModelProperty(CHANGE_DESCRIPTION,
                    designpart.getReference().getDescription().toString());
            Integer timeout = node.getReference().getParameterIntValue(TestDesignTreeParameters.Timeout.name());
            if (timeout != null) {
                setModelProperty(CHANGE_TIMEOUT,
                        timeout);
            }
            String browserType = node.getReference().getParameterStringValue(TestDesignTreeParameters.BrowserType.name());
            Integer browserSpeed = node.getReference().getParameterIntValue(TestDesignTreeParameters.BrowserSpeed.name());

            setModelProperty(CHANGE_BROWSER_SPEED,
                    browserSpeed);
            setModelProperty(CHANGE_BROWSER_TYPE,
                    browserType);
            String deviceIp = node.getReference().getParameterStringValue(TestDesignTreeParameters.DeviceIp.name());

            if (deviceIp != null) {
                setModelProperty(
                        CHANGE_DUT_IP, deviceIp);
            }
            Integer pcId = node.getReference().getParameterIntValue(TestDesignTreeParameters.PcId.name());
            Integer deviceId = node.getReference().getParameterIntValue(TestDesignTreeParameters.DeviceId.name());
            TestPcRefined pcData = null;
            if (pcId != null) {
                pcData = TestPcFactory.newInstance().getTestPc(pcId);
            }
            if (deviceId != null && pcData != null) {
                setModelProperty(CHANGE_PC,
                        pcData.getId());
                DutRefined dut = pcData.getDut(deviceId);
                if (dut != null) {
                    setModelProperty(CHANGE_DUT,
                            dut.getId());
                }
            } else {
                System.out.println("Not found dut device for test plan");
            }
            Integer languageId = node.getReference().getParameterIntValue(TestDesignTreeParameters.LanguageId.name());
            if (languageId != null) {
                setModelProperty(
                        CHANGE_LANGUAGE_ID, languageId);


            }
            Integer profileId = node.getReference().getParameterIntValue(TestDesignTreeParameters.ProfileId.name());
            if (profileId != null) {
                setModelProperty(
                        CHANGE_PROFILE_ID, profileId);


            }
            Integer testBedId = node.getReference().getParameterIntValue(TestDesignTreeParameters.TestBedId.name());
            if (testBedId != null) {
                setModelProperty(
                        CHANGE_TEST_BED, testBedId);
            }
            m_selectedNode = node;
            setModelProperty(SHOW_ADD_TEST_PLAN,
                    true);
        }
    }

    public void operationChangePc(Integer pc) {
        setModelProperty(CHANGE_PC,
                pc);
    }

    public void operationChangeTestBed(Integer testBed) {
        setModelProperty(CHANGE_TEST_BED,
                testBed);
    }

    public void operationChangeLanguageId(Integer languageId) {
        setModelProperty(CHANGE_LANGUAGE_ID,
                languageId);
    }

    public void operationChangeProfileId(Integer val) {
        setModelProperty(CHANGE_PROFILE_ID,
                val);
    }

    public List<DutRefined> operationGetDutList() {
        Integer pcId = (Integer) getModelProperty(CHANGE_PC);
        return operationGetDutList(pcId);
    }

    public List<DutRefined> operationGetDutList(Integer pcId) {
        TestPcRefined ref = TestPcFactory.newInstance().getTestPc(pcId);
        if (ref == null) {
            return null;
        }
        return ref.getDutList();
    }

    public List<TestPcRefined> operationGetPcList() {
        return TestPcFactory.newInstance().getTestPcList();
    }

    public String[] operationGetBrowserTypes() {
        String[] result = {"chrome", "iehta", "firefox", "iexplore"};
        return result;
    }

    public HashMap operationGetLanguages(Integer pcId, Integer dutId) {
        if (pcId == null || dutId == null || pcId == 0 || dutId == 0) {
            return new HashMap();
        }
        TestPcRefined pc = TestPcFactory.newInstance().getTestPc(pcId);
        if (pc == null) {
            return new HashMap();
        }
        DutRefined dutRef = pc.getDut(dutId);
        if (dutRef != null) {
            Integer deviceAbstractionId = dutRef.getDeviceDefinition().getAbstractGroup().getId();
            return TestDesignControllerList.newInstance().
                    getLanguageHandlerController().operationGetLanguageMap(
                    deviceAbstractionId);
        } else {
            return new HashMap();
        }
    }

    public HashMap operationGetLanguages() {
        Integer pcId = (Integer) getModelProperty(CHANGE_PC);
        Integer dutId = (Integer) getModelProperty(CHANGE_DUT);
        return operationGetLanguages(pcId, dutId);
    }

    public List<DeviceGroupRefined> operationGetTestBeds() {
        return DeviceGroupFactory.newInstance().getDeviceGroupList();
    }

    public void initialize(boolean editMode) {
        TestPlanModel testPlanModel = new TestPlanModel();
        TestPlanView testPlanView = new TestPlanView(TestDesignControllerList.newInstance().
                getParentFrame(),
                true,
                editMode);
        super.addModel(testPlanModel);
        super.addView(testPlanView);
        testPlanView.pack();
    }

    public void operationSaveTestPlan(HashMap<String, Object> params) {
        TestDesignControllerList.newInstance().getTreeController().
                operationHandleSaveClones(this,
                m_selectedNode,
                params);
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
            TreeNode node,
            boolean dynamicSaveOnly) {
        if (dynamicSaveOnly) {
            params.put("dynamicSaveOnly", "true");
        }
        return TestPartSaver.operationSave(params,
                (TestDesignTreeNode) node);
    }

    public void operationAddTestPlan(HashMap<String, Object> params) throws PersistentException {
        TestDesignTreeNode node = TestDesignControllerList.newInstance().
                getTreeController().getSelectedTreeNode();
        TestDesignControllerList.newInstance().getTreeController().operationHandleAdd(this, node, params);
    }

    public void operationChangeName(String name) {
        setModelProperty(CHANGE_NAME,
                name);
    }

    public void operationChangeDescription(String description) {
        setModelProperty(CHANGE_DESCRIPTION,
                description);
    }

    public void operationChangeTimeout(String timeOut) {
        Integer timeout = Integer.parseInt(timeOut);
        setModelProperty(CHANGE_TIMEOUT,
                timeout);
    }

    public void operationChangeBrowserSpeed(Integer browserSpeed) {
        //Integer browserspeed= Integer.parseInt(browserSpeed);
        setModelProperty(CHANGE_BROWSER_SPEED,
                browserSpeed);
    }

    public void operationChangeBrowserSpeed(String browserSpeed) {
        Integer browserspeed = Integer.parseInt(browserSpeed);
        setModelProperty(CHANGE_BROWSER_SPEED,
                browserspeed);
    }

    public void operationChangeBrowserType(String browserType) {
        setModelProperty(CHANGE_BROWSER_TYPE,
                browserType);
    }

    public void operationChangeDut(Integer dut) {
        setModelProperty(CHANGE_DUT,
                dut);
    }

    public void operationCloseWindow() {
        this.cleanAll();
    }

    public void operationChangeDutIp(String dutIp) {
        setModelProperty(CHANGE_DUT_IP,
                dutIp);
    }

    @Override
    public TestDesignTreeNode operationAddObject(HashMap<String, Object> params, TreeNode node, int childIndex) {
        TestDesignTreeNode nodeToAdd = TestPartSaver.operationAdd(params,
                (TestDesignTreeNode) node, childIndex, "TestPlan");

        return nodeToAdd;
    }

    @Override
    public boolean isValidSave(HashMap<String, Object> params, TreeNode node) {
        return true;
    }

    @Override
    public boolean isValidAdd(HashMap<String, Object> params, TreeNode node) {
        return true;
    }
}
